export default {
	//数据
	data() {
		return {
      thatUrl: this.urls.baseUrl,
      validate: {}, //验证对象
			uploadNum: 0,
			FileUpload: [],
      FileUploads: [], // 展示图片
      attachment: '',  // 附件
      picture: ''  // 展示图片
		}
	},
	created() { //调用方法
	},
	mounted() {
    //加载表单验证
    this.validate = $("#baseForm").validate({
      ignore: [],
      rules: {
        name: { // 名称
          required: true,
          maxlength: 50
        },
        type: { // 类型
          required: true,
        },
        address: { // 地址
          required: true,
          maxlength: 50
        },
        unit: { // 申请单位
          required: true,
          maxlength: 50
        },
        contact: { // 联系人
          required: true,
          maxlength: 10
        },
        correspondenceAddress: { // 通讯地址
          required: true,
          maxlength: 50
        },
        theContact: { // 练习方式
          required: true,
          number: true
        },
        financialContact: { // 财务联系人
          required: true,
          maxlength: 10
        },
        contactNumber: { // 财务联系电话
          required: true,
          number: true
        },
        introduction: { // 基地简介
          required: true,
          maxlength: 1000
        },
		picture:{
		   required: true,//基地图片
		}
      },
      messages: {
        name: { // 名称
          required: '基地名称不能为空',
          maxlength: '最大长度为50'
        },
        type: { // 类型
          required: '请选择',
        },
        address: { // 地址
          required: '基地地址不能为空',
          maxlength: '最大长度为50'
        },
        unit: { // 申请单位
          required: '申请单位不能为空',
          maxlength: '最大长度为50'
        },
        contact: { // 联系人
          required: '联系人不能为空',
          maxlength: '最大长度为10'
        },
        correspondenceAddress: { // 通讯地址
          required: '通讯地址不能为空',
          maxlength: '最大长度为50'
        },
        theContact: { // 联系方式
          required: '联系方式不能为空',
          number: '联系方式只能输入数字'
        },
        financialContact: { // 财务联系人
          required: '财务联系人不能为空',
          maxlength: '最大长度为10'
        },
        contactNumber: { // 财务联系电话
          required: '财务联系电话不能为空',
          number: '财务联系电话只能输入数字'
        },
        introduction: { //
          required: '基地简介不能为空',
          maxlength: '基地简介最多只能1000字'
        },
		picture:{ //
          required: '基地展示图片不能为空',
        },
      },
      /*errorElement: "em",
      errorPlacement: function(error, element) {
        error.appendTo(element.parents(".error-box").children(".error-msg"));
      }*/
    });
	},
	methods: { //写函数方法
		/*返回到登录页*/
		jumpPage: function() {
			this.$router.push({
				path: "/registered",
			})
		},
		touchOffIdInput(n) {
			this.uploadNum = n;
			if(n == 1) {
				$("#handIdFront").click();
			}
		},
    /**文件上传(多文件)js**/
    idFrontImg: function(event) {
      let that = this;
      let files = event.currentTarget.files;
      if (!that.fileGlobalFilter(files)) {
        return;
      }
      let formData = new FormData();
      for(let i = 0, len = files.length; i < len; i++) {
        formData.append('fileNames', files[i]);
      }
      let config = {
        headers: {
          'Content-Type': 'multipart/form-data;boundary = ' + new Date().getTime()
        }
      };
      that.$post(
        that.urls.upload,
        formData,
        config
      )
        .then(function(res) {
          if(res.success) {
            let fileInfos = res.data;
            let urls = [];
            let names = [];
            for(let i = 0, len = fileInfos.length; i < len; i++) {
              urls.push(fileInfos[i].url);
              names.push(that.getFileName(fileInfos[i].url));
            }
            that.FileUpload = names;
            that.attachment = urls.join(";");
          }
        })
      console.log(that.FileUpload)
    },
    // 展示图片
    touchOffIdInputs(n) {
      this.uploadNum = n;
      if(n == 1) {
        $("#handIdFronts").click();
      }
    },
    /**文件上传(多文件)js  图片 **/
    idFrontImgs: function(event) {
      let that = this;
      let files = event.currentTarget.files;
      if (!that.fileGlobalFilter(files)) {
        return;
      }
      let formData = new FormData();
      for(let i = 0, len = files.length; i < len; i++) {
        formData.append('fileNames', files[i]);
      }
      let config = {
        headers: {
          'Content-Type': 'multipart/form-data;boundary = ' + new Date().getTime()
        }
      };
      that.$post(
        that.urls.upload,
        formData,
        config
      )
        .then(function(res) {
          if(res.success) {
            let fileInfos = res.data;
            let urls = [];
            let names = [];
            for(let i = 0, len = fileInfos.length; i < len; i++) {
              urls.push(fileInfos[i].url);
              names.push(that.getFileName(fileInfos[i].url));
            }
            that.FileUploads = names;
            that.picture = urls.join(";");

            $('#pictureImg').attr('src', that.thatUrl + that.picture);
            that.validate.resetForm();
          }
        })
      console.log()
    },
    // 提交表单
    subForm: function () {
      let that = this;
      let flg = that.validate.form();
      if(flg) {

 /*       if(that.picture == ''){
          layer.msg('请上传基地展示图片!', {
            time: 3000, //3s后自动关闭
          });
          return;
        }*/

        that.$post(
          that.urls.baseQueryHeavy,
          {"name": $('#baseName').val()}
        )
          .then(function(res) {
            if( ! res.success) {
              console.log(res.data)

                let   dataObj = that.combinationFormObject(that.validate.currentForm);
                dataObj.status = 4;
                that.$post(
                  that.urls.addBaseAreas,
                  dataObj
                )
                  .then(function(res) {
                    if(res.success) {
                      that.modelShowTeachers = false;
                      layer.msg('成功提交基地入驻申请', {
                        time: 3000, //3s后自动关闭
                      });
                      that.$router.push({
                        path: "index",
                      })

                    }else {
                      layer.msg('提交基地入驻申请失败', {
                        time: 3000, //3s后自动关闭
                      });
                    }
                  })
            }else {


                layer.msg('基地名称已存在，请重新输入!', {
                  time: 3000, //3s后自动关闭
                });


            }


          })
          }
      },
   // 附件删除
    delAtt: function () {
      this.attachment = '';
      this.FileUpload = [];
    },

    // 模板下砸
    getBaseTem: function () {
      let that = this;
      that.$post(
        that.urls.getTemplate,
        {"type": 1}
      )
        .then(function(res) {
          if(res.success) {

            that.downloadResources(res.data.url);
          }else {
            layer.msg('请联系管理员上传【基地申请书】', {
              time: 3000, //3s后自动关闭
            });
          }
        })

    },


    //下载资源
    downloadResources: function(filePath) {
      console.log(filePath)
      if(filePath != null && filePath != "") {
        window.location.href = this.urls.download + "?path=" + filePath;
      }else {
        layer.msg('暂无附件!', {
          time: 3000, //3s后自动关闭
        });
      }
    },

	}
}
